/*
* Copyright (c) 2011 Mladen Milankovic <mmlado at gmail.com>.
*
* This file is part of Demine application
*
* Demine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Demine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with Demine.  If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 1.1
import com.nokia.meego 1.0
import QtMobility.feedback 1.1
import "../core"

Page {
    id: game

    property int columns: 10
    property int rows: 10
    property int mines: 14
    property int flags: 0
    property int fieldSize: (width > height ? width : height) / 10
    property int time: 0
    property bool running: false

    signal end(bool won)

    /*HapticsEffect {
        id: rumbleEffect
        attackIntensity: 0.0
        attackTime: 250
        intensity: 1.0
        duration: 100
        fadeTime: 250
        fadeIntensity: 0.0
    }*/

    Rectangle {
        color: table.color
        anchors.fill: parent
        /*PinchArea {
            //width: table.width
            //height: table.height
            anchors.fill: parent
            pinch.target: table
            //enabled: true
            pinch.minimumScale: 1
            pinch.maximumScale: 2
            //pinch.dragAxis: Pinch.XandYAxis

            /*onPinchStarted: {
                //console.debug(table.height*table.scale);
                //console.debug(pinch.scale);
                if (pinch.scale > 2) {
                    pinch.scale = 2
                }
                if (pinch.scale < 1) {
                    pinch.scale = 1
                }
                flickable.contentHeight = table.height*pinch.scale;
                flickable.contentWidth = table.width*pinch.scale;
            }*/

            /*onPinchUpdated: {
                //console.debug(table.height*table.scale);
                console.debug(pinch.scale);
                if (flickable.contentWidth * pinch.scale < main.width) return;
                if (flickable.contentWidth * pinch.scale > main.width*2) return;
                flickable.contentHeight = main.width*pinch.scale;
                flickable.contentWidth = main.width*pinch.scale;
            }

            onPinchFinished: {
                //main.contentX = table.x
                //main.contentY = table.y
                console.debug(table.height * table.scale);
                console.debug(table.width * table.scale);

                flickable.contentHeight = table.height * table.scale;
                flickable.contentWidth = table.width * table.scale;
            }
        }*/

        Flickable {
            id:  flickable
            anchors.fill: parent
            contentWidth: game.fieldSize * game.columns
            contentHeight: game.fieldSize * game.rows
            Table {
                id: table
                /*width: (main.width / 10) * (columns * (1 + table.spacing) )
                height: (main.width / 10) * (rows * (1 + table.spacing) )*/
                onLost: game.lost()
                onWon: game.won()
                columns: game.columns
                rows: game.rows
                numOfMines: game.mines
                fieldSize: game.fieldSize
                onFlag: {
                    game.flags = number;
                    //rumbleEffect.start();
                }
                onFirstOpen: {
                    running = true;
                    tmrTime.start();
                }
            }
        }

        ScrollDecorator { flickableItem: flickable }
    }

    Timer {
        id: tmrTime
        interval: 1000
        running: false
        repeat: true
        onTriggered: {
            timeCount.text = humanizeTime(++game.time);
        }
    }

    function humanizeTime(seconds) {
        var hours = Math.floor(seconds / (60 * 60));
        seconds -= hours * 60 * 60;
        var minutes = Math.floor(seconds / 60);
        seconds -= minutes * 60;
        if (!hours) return '' + ((minutes >= 10) ? minutes : '0' + minutes) + ':' + ((seconds >= 10) ? seconds : '0' + seconds);
        return '' + ((hours >= 10) ? hours : '0' + hours) + ':' + ((minutes >= 10) ? minutes : '0' + minutes) + ':' + ((seconds >= 10) ? seconds : '0' + seconds);
    }

    function getTime() {
        return humanizeTime(game.time);
    }

    function initialize() {
        table.initialize();
    }

    function newGame() {
        table.newGame();
        flickable.contentX = 0;
        flickable.contentY = 0;
        flags = 0;
        time = 0;
        timeCount.text = humanizeTime(game.time);
        tmrTime.stop();
    }

    function pause() {
        if (!running) return false;
        tmrTime.stop();
    }

    function resume() {
        if (!running) return false;
        tmrTime.start();
    }

    function lost() {
        tmrTime.stop();
        running = false;
        end(false);
    }

    function won() {
        tmrTime.stop();
        running = false;
        end(true);
    }
}
